<?php
namespace Tongcheng\Controller;
use Think\Controller;
class GoodsController extends CommonController {
    public function index(){

        if(IS_AJAX){
            $model = M("Goods");
            $map = array();
            $map['wid'] = session('wid');
            $map['status'] = 1;
            // $map['is_pay'] = 1;

            $res['count'] = $model->where($map)->count();
            $page = $_GET['page']?$_GET['page']:1;
            $limit = $_GET['limit']?$_GET['limit']:10;
            $list = $model->where($map)->page("{$page},{$limit}")->select();
            foreach ($list as $k => $v) {
                $eq['shop_id'] = $v['shop_id'];
                $v['shop_name'] = M("Shop")->where($eq)->getField("name");
                $list[$k] = $v;

                $tui = M("tui")->where(['wid'=>session('wid'),'aid'=>$v['id'],"type"=>'goods'])->find();
                if(!$tui || !$tui['status']){
                    $list[$k]['is_tui'] = 0;
                }else{
                    $list[$k]['is_tui'] = 1;
                }
            }
            $res['list']= $list;

            $this->ajaxReturn($res);
        }else{
            $this->display();
        }  
    }

    // public function add(){
    // 	$model = M("InfoCate");
    // 	$map['wid'] = session('wid');
    // }

    // public function edit(){
    // 	$model = M("InfoCate");
    // 	$map['wid'] = session('wid');
    // }

    public function tui(){
        $r = tui("goods",I('id'),session('wid'),$_POST['status']);
        if($r){
            $this->success("操作成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    public function del(){
    	$model = M("Goods");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        $new['status'] = 0;

        if($model->where($map)->save($new)){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    public function edit(){
        $goodsM = M("Goods");
        $shopM = M("shop");
        $id = $_GET['id'];
        $list = $goodsM->field('tp_goods.*, tp_shop.shop_id')->join('tp_shop on tp_goods.shop_id = tp_shop.shop_id')->where(['tp_goods.id' => $id])->find();
        //商家
        $shop = $shopM->field('shop_id, name')->where(['wid' => $list['wid']])->select();
        $this->assign('shop', $shop);
        $this->assign('list', $list);
        $this->display();
    }

    public function update() {
        $goodsM = M("Goods");
        $id = $_POST['id'];
        $date['shop_id'] = $_POST['shop_id'];
        $date['title'] = $_POST['title'];
        $date['standard'] = $_POST['standard'];
        $date['price'] = $_POST['price'];
        $date['content'] = $_POST['content'];
        $row = $goodsM->where(['id' => $id])->save($date);
        $this->ajaxReturn($row);
    }

    //设置商品抽成
    public function chou()
    {
        $eq['id'] = I('id');
        $data['chou'] = I('chou');
        $re = M("goods")->where($eq)->save($data);
        if ($re) {
            $this->success('修改成功');
        } else {
            $this->error('网络繁忙');
        }
    }
}